package com.cbank.utils;

import com.cbank.domain.ModuleShowDetail;
import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;

import java.util.List;

/**
 * Created by threenoodles on 2015/9/21.
 */

/**
 * String 工具类
 */
public class StringUtil {

	private static Logger logger = Logger.getLogger(StringUtil.class);
	
    public static final String EMPTY_STRING = "";
    private StringUtil() {

    }

    /**字符串是否为空
     * @param str
     * @return
     */
    public static boolean isEmpty(String str){
        if(str == null || str.length() <= 0){
            return true;
        }else {
            return false;
        }
    }
    public static boolean isNotEmpty(String str){

        return !StringUtil.isEmpty(str);
    }

    /**字符串trim 后是否为空
     * @param str
     * @return
     */
    public static boolean isBlank(String str){
        if(str == null || str.trim().length() <= 0){
            return true;
        }else {
            return false;
        }
    }
    public static boolean isNotBlank(String str){

        return !StringUtil.isBlank(str);
    }

    /*string 转换成 long*/
    public static long toLongValue(Object obj){
    	if(obj == null){
    		return 0;
    	}
        if(obj instanceof Long){
            return (Long) obj;
        }
        String str = null;
        if(obj instanceof String){
            str = (String) obj;
        }else {
            str = String.valueOf(obj);
        }
        if(StringUtil.isBlank(str)){
            return 0;
        }else if("null".equals(str)){
        	return 0;
        }
        try{
            return Long.valueOf(str);
        }catch (Exception e){
        	logger.error(e.getMessage(),e);
        }
        return 0;
    }

    /*string 转换成 long*/
    public static int toIntValue(Object obj){
        if(obj == null){
            return 0;
        }
        if(obj instanceof Integer){
            return (Integer) obj;
        }
        String str = null;
        if(obj instanceof String){
            str = (String) obj;
        }else {
            str = String.valueOf(obj);
        }
        if(StringUtil.isBlank(str)){
            return 0;
        }else if("null".equals(str)){
        	return 0;
        }
        try{
            return Integer.valueOf(str);
        }catch (Exception e){
        	logger.error(e.getMessage(),e);
        }
        return 0;
    }
    

    /**字符串trim
     * @param str
     * @return
     */
    public static String trim(String str){
        if(str == null || str.length() <= 0){
            return StringUtil.EMPTY_STRING;
        }else {
            return str.trim();
        }
    }
    
    public static void main(String[] args) {
		
    	System.out.println(StringUtil.toLongValue("null"));
	}

    
	private static boolean isNotEmojiCharacter(char codePoint) {
		return (codePoint == 0x0) || (codePoint == 0x9) || (codePoint == 0xA) || (codePoint == 0xD)
				|| ((codePoint >= 0x20) && (codePoint <= 0xD7FF)) || ((codePoint >= 0xE000) && (codePoint <= 0xFFFD))
				|| ((codePoint >= 0x10000) && (codePoint <= 0x10FFFF));
	}
    /**
	 * 过滤emoji 或者 其他非文字类型的字符
	 * 
	 * @param source
	 * @return
	 */
	public static String filterEmoji(String source) {
		int len = source.length();
		StringBuilder buf = new StringBuilder(len);
		for (int i = 0; i < len; i++) {
			char codePoint = source.charAt(i);
			if (isNotEmojiCharacter(codePoint)) {
				buf.append(codePoint);
			} else {
				buf.append("");
			}
		}
		return buf.toString();
	}

	public static List<ModuleShowDetail> subActrive(List<ModuleShowDetail> moduleShowDetailList){
        for (ModuleShowDetail moduleShowDetail : moduleShowDetailList) {
            String cnActrive=subModuleShowDetailString(moduleShowDetail.getCnArticle());
            String enActrive=subModuleShowDetailString(moduleShowDetail.getEnArticle());
            moduleShowDetail.setEnArticle(enActrive);
            moduleShowDetail.setCnArticle(cnActrive);
        }

	    return moduleShowDetailList;
    }

    public static String subModuleShowDetailString(String actrive){
        String[] strings=actrive.split("</iframe>");
        for (int i = 0; i < strings.length; i++) {
            if (i !=strings.length){
                strings[i]=strings[i]+"</iframe>";

                String[] strs=strings[i].split("<iframe");
                String src="";
                String widht="";
                String height="";
                for (int j = 0; j < strs.length; j++) {
                    if (j != 0){
                        strs[j]="<iframe"+strs[j];
                        if (strs[j].contains("<iframe")){
                            String[] strs1=strs[j].split(" ");
                            for (String s : strs1) {
                                if (s.contains("src")){
                                    int srcStart=s.indexOf("src=\"");
                                    src=s.substring(srcStart+5,s.length()-1);
                                }
                                if (s.contains("width")){
                                    int widthStart=s.indexOf("width=\"");
                                    widht=s.substring(widthStart+7,s.length()-1);
                                }
                                if (s.contains("height")){
                                    int heightStart=s.indexOf("height=\"");
                                    height=s.substring(heightStart+8,s.length()-1);
                                }
                            }
                        }
                    }

                }
                if (strings[i].contains("<iframe")){
                    int start=strings[i].indexOf("<iframe");
                    String iframr=strings[i].substring(start);
                    String newIframe="<video controls name=\"\" style=\"width:"+widht+"px; height:"+height+"px\"> <source src="+src+" type=\"video/mp4\"></video>";
                    strings[i]=strings[i].replaceAll(iframr,newIframe);
                }
            }

        }
        actrive=StringUtils.join(strings);
        return actrive;
    }
}
